package mn.more.wits.server.dao.temp;

import java.io.IOException;
import javax.servlet.ServletContext;
import mn.more.mock.io.MockModel;
import mn.more.mock.io.xml.XMLReader;
import mn.more.mock.io.xml.XMLWriter;
import org.apache.log4j.Logger;

/**
 * @author <a href="mailto:mike.liu@aptechmongolia.edu.mn">Mike Liu</a>
 * @version $Revision: 119 $
 */
public abstract class AbstractTestDataDAO {

	protected MockModel rootModel;
	protected XMLReader reader;
	protected XMLWriter writer;
	protected ServletContext context;
	protected boolean debug;

	protected abstract Logger getLogger();

	public void setReader(XMLReader reader) { this.reader = reader; }

	public void setWriter(XMLWriter writer) { this.writer = writer; }

	public void setContext(ServletContext context) { this.context = context; }

	public void setDebug(boolean debug) { this.debug = debug; }

	/** provide publically accessible method to refresh the data models from the XML file */
	public void refreshData() {
		Logger logger = getLogger();
		if (logger.isDebugEnabled()) { logger.debug("refreshing models in " + reader.getFilename() + "..."); }

		// NOTE: this code is not synchronized because it is NEVER meant to be used in production mode.
		// As such, the code below might cause error during high load - esp. when there's a lot of changes to user
		// model and servlet context (where user info. is stored).
		try {
			reader.read();
			if (logger.isDebugEnabled()) { logger.debug("model data refresh complete"); }
			rootModel = reader.getModel("_all");
		} catch (IOException e) {
			throw new RuntimeException("Unable to read model data from XMLReader: " + e.getMessage());
		}
	}
}
